using UnityEngine;
using UnityEngine.UI;
using QFramework;
using System.Collections.Generic;

namespace ProjectSurvivor
{
	public class UIGamePlanelData : UIPanelData
	{
	}
	public partial class UIGamePlanel : UIPanel,IController
	{
		public static EasyEvent FlashSreen = new EasyEvent();
		public static EasyEvent OpenTreasurePanel = new EasyEvent();
		public static EasyEvent treasureChestPanel = new EasyEvent();
		protected override void OnInit(IUIData uiData = null)
		{
			mData = uiData as UIGamePlanelData ?? new UIGamePlanelData();
			//please add init code here
			Global.Exp.RegisterWithInitValue(expnum =>
			{
				ExpValue.fillAmount = expnum / (float)Global.ExpToNextLevel();
				//expText.text = "经验值:(" + expnum + "/" + Global.ExpToNextLevel() + ")";
				if (expnum >= Global.ExpToNextLevel())//判断经验有没有达到
				{
					Global.Exp.Value -= Global.ExpToNextLevel();
					Global.Level.Value++;
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			ExpUpgrdelPlanel.Hide();
			Global.Level.Register(levelnum =>
			{
				levelText.text = "等级:" + levelnum;
				ExpUpgrdelPlanel.Show();
				Time.timeScale = 0;
			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			Global.CurrentSeconds.Register(currentSeconds =>
			{
				if (Time.frameCount % 30 == 0)//每30执行一次
				{
					var secondsInt = Mathf.FloorToInt(currentSeconds);
					var seconds = secondsInt % 60;
					var minutes = secondsInt / 60;
					timeText.text = "时间:" + $"{minutes:00}:{seconds:00}";
				}

			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			//Global.tipe.RegisterWithInitValue(currentSeconds =>
			//{
			//	if(currentSeconds!=-1)
			//	{
   //                 UnlockedkediconRoot.Show();
			//		Global.tipe.Value = -1;
   //             }
            




   //         }).UnRegisterWhenGameObjectDestroyed(gameObject);

			Global.Coin.RegisterWithInitValue(coin =>
			{
				CoinText.text = "金币:" + coin;
			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			var EnemyGenerator = FindObjectOfType<EnemyGenerator>();
			EnemyGenerator.EnemyCount.RegisterWithInitValue(enemyCountnum =>
			{
				enemyCount.text = "敌人:" + enemyCountnum;
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			ActionKit.OnUpdate.Register(() =>//注册帧更新/UI调试回调；
			{
				Global.CurrentSeconds.Value += Time.deltaTime;
				if (EnemyGenerator.LastWave && EnemyGenerator.CurrentWave == null && !FindObjectOfType<Enemy>(false) && EnemyGenerator.EnemyCount.Value == 0 && Global.Exp.Value != 0)
				{
					UIKit.OpenPanel<UIGamePassPlanel>();//打开 UI
					Time.timeScale = 0;
				}
			});
			this.GetSystem<CoinUpgradeSystem>().Say();
			this.GetSystem<ExpUpgrdeSystem>().Say();
			FlashSreen.Register(() =>
			{
				ActionKit
				.Sequence()
				.Lerp(0, 0.5f, duration: 0.1f, alpha =>
				{
					ScreenColor.ColorAlpha(alpha);
				})
				.Lerp(0.5f, 0, duration: 0.2f, alpha =>
				{
					ScreenColor.ColorAlpha(alpha);
				}, () => ScreenColor.ColorAlpha(0))
				.
				Start(monoBehaviour: this);
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			OpenTreasurePanel.Register(() =>
			{
				Time.timeScale = 0;
				TreasureChesPanel.Show();
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			treasureChestPanel.Register(() =>
			{
				Time.timeScale = 0;
				TreasureChestPanel.Show();
			});

        }
		
		protected override void OnOpen(IUIData uiData = null)
		{
		}
		
		protected override void OnShow()
		{
		}
		
		protected override void OnHide()
		{
		}
		
		protected override void OnClose()
		{
		}

        public IArchitecture GetArchitecture()
        {
			return Global.Interface;//初始化（确保系统注册）。
        }
    }
}
